package com.example.argumentresolver.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class ConfigAdapter extends WebMvcConfigurerAdapter {
    @Autowired
    private UserInfoResolver userInfoResolver;

    @Autowired
    RequestMappingHandlerAdapter adapter;

    /**
     * 当存在@RequestBody时，我们自定义的HandlerMethodArgumentResolver会不生效
     * 可以通过下面方式，将UserInfoResolver添加到队列头部，就会生效
     */
    @PostConstruct
    public void post() {
//        List<HandlerMethodArgumentResolver> argumentResolvers = adapter.getArgumentResolvers();
//        List<HandlerMethodArgumentResolver> customResolvers = adapter.getCustomArgumentResolvers();
//        if (customResolvers != null) {
//            argumentResolvers.removeAll(customResolvers);
//            argumentResolvers.addAll(0, customResolvers);
//        }
//        // 自定义的Base64RequestMethodArgumentsResolver前置
//        argumentResolvers.add(0, userInfoResolver);
//        adapter.setArgumentResolvers(argumentResolvers);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        // 建议通过这种方式，可以将userInfoResolver添加到容器中
        argumentResolvers.add(userInfoResolver);
//        argumentResolvers.add(new UserInfoResolver());
    }
}